﻿using System.Collections.ObjectModel;
using Catel.Articles._03___MVVM.Models;
using Catel.Data;
using Catel.MVVM;

namespace Catel.Articles._03___MVVM.Examples.NestedUserControls
{
    /// <summary>
    /// HouseWindow view model.
    /// </summary>
    public class HouseWindowViewModel : ViewModelBase
    {
        #region Variables
        private int _roomIndex = 1;
        #endregion

        #region Constructor & destructor
        /// <summary>
        /// Initializes a new instance of the <see cref="HouseWindowViewModel"/> class.
        /// </summary>
        public HouseWindowViewModel()
        {
            // Create commands
            AddRoom = new Command<object>(AddRoom_Execute);

            // Create house
            House = new House();
        }
        #endregion

        #region Properties
        /// <summary>
        /// Gets the title of the view model.
        /// </summary>
        /// <value>The title.</value>
        public override string Title { get { return "House"; } }

        #region Models
        /// <summary>
        /// Gets or sets the house.
        /// </summary>
        [Model]
        public House House
        {
            get { return GetValue<House>(HouseProperty); }
            private set { SetValue(HouseProperty, value); }
        }

        /// <summary>
        /// Register the House property so it is known in the class.
        /// </summary>
        public static readonly PropertyData HouseProperty = RegisterProperty("House", typeof(House));
        #endregion

        #region View model
        /// <summary>
        /// Gets or sets the name.
        /// </summary>
        [ViewModelToModel("House")]
        public string Name
        {
            get { return GetValue<string>(NameProperty); }
            set { SetValue(NameProperty, value); }
        }

        /// <summary>
        /// Register the Name property so it is known in the class.
        /// </summary>
        public static readonly PropertyData NameProperty = RegisterProperty("Name", typeof(string));

        /// <summary>
        /// Gets or sets the room collection.
        /// </summary>
        [ViewModelToModel("House")]
        public ObservableCollection<Models.Room> Rooms
        {
            get { return GetValue<ObservableCollection<Models.Room>>(RoomsProperty); }
            set { SetValue(RoomsProperty, value); }
        }

        /// <summary>
        /// Register the Rooms property so it is known in the class.
        /// </summary>
        public static readonly PropertyData RoomsProperty = RegisterProperty("Rooms", typeof(ObservableCollection<Models.Room>));
        #endregion
        #endregion

        #region Commands
        /// <summary>
        /// Gets the AddRoom command.
        /// </summary>
        public Command<object> AddRoom { get; private set; }

        /// <summary>
        /// Method to invoke when the AddRoom command is executed.
        /// </summary>
        /// <param name="parameter">The parameter of the command.</param>
        private void AddRoom_Execute(object parameter)
        {
            // Add a room
            Rooms.Add(new Models.Room(string.Format("Room {0}", _roomIndex++)));
        }
        #endregion

        #region Methods
        #endregion
    }
}
